import { apiOrderXcCode } from '../../api/index'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    isLoaded: false,
    goodsHeight: 0,
    bgImg: ''
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad (options) {
    // options.id
    this.screenRatio = wx.getSystemInfoSync().windowWidth / 375
    apiOrderXcCode({ order_share_id: options.id }).then(
      res => {
        this.creatImg(res.data)
      }
    )
  },
  creatImg (data) {
    wx.showLoading({
      title: '拼命生成中'
    })
    wx.getImageInfo({
      src: data[0],
      success: (res) => {
        const width = res.width / 3
        const height = res.height / 3
        this.setData({
          goodsHeight: height * this.screenRatio
        })
        const ctx = wx.createCanvasContext('myCanvas')
        // 底图
        ctx.drawImage(res.path, 0, 0, width * this.screenRatio, height * this.screenRatio)
        ctx.beginPath()
        ctx.setTextAlign('center') // 文字居中
        ctx.setFillStyle('#FDDDBB')
        ctx.setFontSize(14)
        ctx.fillText('预计1～2名新用户下单即可完成接力', 167 * this.screenRatio, 430 * this.screenRatio)
        ctx.fillText('长按扫码接力', 167 * this.screenRatio, 560 * this.screenRatio)
        ctx.stroke()
        wx.getImageInfo({
          src: wx.getStorageSync('avatarUrl'),
          success: (userImg) => {
            // 绘制头像
            ctx.save()
            ctx.beginPath() // 开始绘制
            ctx.arc(175.5 * this.screenRatio, 47.5 * this.screenRatio, 22.5 * this.screenRatio, 0, Math.PI * 2)
            ctx.clip()
            ctx.drawImage(userImg.path, 153 * this.screenRatio, 25 * this.screenRatio, 45 * this.screenRatio, 45 * this.screenRatio)
            ctx.restore()
            wx.getImageInfo({
              src: data[1],
              success: (eq) => {
                ctx.drawImage(eq.path, 135 * this.screenRatio, 460 * this.screenRatio, 68 * this.screenRatio, 68 * this.screenRatio)
                ctx.draw(false, (success) => {
                  if (success.errMsg.indexOf('ok') !== -1) {
                    wx.canvasToTempFilePath({
                      x: 0,
                      y: 0,
                      canvasId: 'myCanvas',
                      success: (res) => {
                        this.fileImgPath = res.tempFilePath
                        wx.hideLoading()
                        this.setData({
                          isLoaded: true,
                          canvasImg: res.tempFilePath
                        })
                      }
                    })
                  }
                })
              }
            })
          }
        })
      }
    })
  },
  saveImage () {
    wx.saveImageToPhotosAlbum({
      filePath: this.fileImgPath,
      success: () => {
        wx.showToast({
          title: '保存成功'
        })
      },
      fail: (err) => {
        if (err.errMsg === 'saveImageToPhotosAlbum:fail:auth denied' || err.errMsg === 'saveImageToPhotosAlbum:fail auth deny') {
          wx.showModal({
            title: '提示',
            content: '需要您授权保存相册',
            showCancel: false,
            success: (sure) => {
              wx.openSetting({
                success: (set) => {
                  if (set.authSetting['scope.writePhotosAlbum']) {
                    wx.showModal({
                      title: '提示',
                      content: '获取权限成功,再次点击图片即可保存',
                      showCancel: false
                    })
                  } else {
                    wx.showModal({
                      title: '提示',
                      content: '获取权限失败，将无法保存到相册哦~',
                      showCancel: false
                    })
                  }
                }
              })
            }
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow () {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload () {

  }
})
